 MPL tips_files/figure-html/cell-2-output-1.png)
matplotlib 미세먼지팁
그림만 보고 싶을때
marker size, line width
label + legend
색깔조정 (C0,C1,…)
title 설정
- (방법1)
plt.plot([1,2,3,4],[1,2,3,2],'--o',label='A',color='C1')
plt.plot([1,2,3,4],[3,2.1,1,3],'--o',label='B',color='C0')
plt.legend()
plt.title('title')Text(0.5, 1.0, 'title')
 MPL tips_files/figure-html/cell-7-output-2.png)
- (방법2)
suptitle 설정
fig, ax = plt.subplots(2,2)
ax[0,0].plot([1,2,3,2],'--o',label='A',color='C0')
ax[0,0].set_title('(a)')
ax[0,1].plot([3,2.1,1,3],'--o',label='B',color='C1')
ax[0,1].set_title('(b)')
ax[1,0].plot([-3,-2.1,-1,-3],'--o',label='B',color='C2')
ax[1,0].set_title('(c)')
ax[1,1].plot([3,-2.1,1,-3],'--o',label='B',color='C3')
ax[1,1].set_title('(d)')
#plt.suptitle('suptitle')
fig.suptitle('suptitle')Text(0.5, 0.98, 'suptitle')
 MPL tips_files/figure-html/cell-9-output-2.png)
tight_layout()
fig, ax, plt 소속
- 일단 그림 하나 그리고 이야기좀 해보자.
- fig에는 있고 ax에는 없는 것
add_axes, tight_layout, suptitle, …
- ax에는 있고 fig에는 없는 것
boxplot, hist, plot, set_title, …
- plt는 대부분 다 있음. (의미상 명확한건 대충 알아서 fig, ax에 접근해서 처리해준다) - plt.tight_layout, plt.suptitle, plt.boxplot, plt.hist, plot.plot - plt.set_title 은 없지만 plt.title 은 있음 - plt.add_axes 는 없음..
x축, y축 label 설정
ax.xaxis.set_label_text('xlabel',size=16,family='serif',weight=1000,style='italic')
#_fontsettings={'size':16,'family':'serif','weight'=1000,'style':'italic'}
#ax.xaxis.set_label_text('xlabel',_fontsettings)
fig MPL tips_files/figure-html/cell-14-output-1.png)
폰트ref - size: - fontweight: 0~1000 - family: ‘serif’, ‘sans-serif’, ‘monospace’ - style: ‘normal’, ‘italic’
Latex
- 예시1
x1= np.linspace(-2,2,1000)
y1= (x1-1)**2
fig, ax = plt.subplots()
ax.plot(x1,y1,'--')
ax.set_title('$y_1=(x_1-1)^2$')Text(0.5, 1.0, '$y_1=(x_1-1)^2$')
 MPL tips_files/figure-html/cell-17-output-2.png)
- 예시2
x1 = np.linspace(-2,2,1000)
y1 = 0.5*(x1-1)**2
fig, ax = plt.subplots()
ax.plot(x1,y1,'--')
ax.set_title(r'$y_1=\frac{1}{2}(x_1-1)^2$',size=20); MPL tips_files/figure-html/cell-18-output-1.png)
- 예시3
x1 = np.linspace(-2,2,1000)
y1 = 0.5*(x1-1)**2
fig, ax = plt.subplots()
ax.plot(x1,y1,'--')
ax.set_title(r'$y_1=\frac{1}{2}(x_1-1)^2$',size=20)
ax.set_xlabel(r'$x_1$',size=15)
ax.set_ylabel(r'$y_1$',size=15); MPL tips_files/figure-html/cell-19-output-1.png)
- 예시4
fig.subplots()
plt.subplot
- 끝에 s가 없어요!!
- 기능1: 몰라도 됩니당.. (아마도)
- 기능2: 특이해요.. fig를 안받아도 무방함
ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(223)
ax4 = plt.subplot(224)
ax1.plot([1,2,4,3],'o',color='C0');ax1.set_title('221')
ax2.plot([1,2,4,3],'o',color='C1');ax2.set_title('222')
ax3.plot([1,2,4,3],'o',color='C2');ax3.set_title('223')
ax4.plot([1,2,4,3],'o',color='C3');ax4.set_title('224')
fig=plt.gcf()
fig.suptitle("plt.subplot(22x)",size=15)
fig.tight_layout() MPL tips_files/figure-html/cell-23-output-1.png)
위는 아래와 같은 코드임
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax1.plot([1,2,4,3],'o',color='C0');ax1.set_title('221')
ax2.plot([1,2,4,3],'o',color='C1');ax2.set_title('222')
ax3.plot([1,2,4,3],'o',color='C2');ax3.set_title('223')
ax4.plot([1,2,4,3],'o',color='C3');ax4.set_title('224')
fig.tight_layout()
fig.suptitle("fig.add_subplot(22x)",size=15)
fig.tight_layout() MPL tips_files/figure-html/cell-24-output-1.png)
- fig.add_subplot() vs fig.add_axes()
- fig.add_subplot(): 입력으로 nrows, ncols, index 전달 (편하게 쓰기엔 좋아)
- fig.add_axes(): 입력으로 left, bottom, width, height 전달 (이상한 그래프 만들기 좋아)
- plt.subplots() vs plt.subplot()
- plt.subplots(): 넣을 수 있는 액시즈 종류가 한가지
- plt.subplot(): 여러 (특이한) 액시즈를 넣을 수 있음
(기본액시즈)
(3d 액시즈)
ax=plt.subplot(111,projection='3d')
ax.plot([1,2,3,4],[1,2,-3,4],[1,2,-3,-4],'--o')
fig=plt.gcf()
fig.set_figheight(12) MPL tips_files/figure-html/cell-26-output-1.png)
(polar 액시즈)
 MPL tips_files/figure-html/cell-3-output-1.png)
 MPL tips_files/figure-html/cell-4-output-1.png)
 MPL tips_files/figure-html/cell-5-output-2.png)
 MPL tips_files/figure-html/cell-6-output-2.png)
 MPL tips_files/figure-html/cell-8-output-2.png)
 MPL tips_files/figure-html/cell-10-output-1.png)
 MPL tips_files/figure-html/cell-12-output-1.png)
 MPL tips_files/figure-html/cell-13-output-1.png)
 MPL tips_files/figure-html/cell-15-output-1.png)
 MPL tips_files/figure-html/cell-20-output-2.png)
 MPL tips_files/figure-html/cell-21-output-1.png)
 MPL tips_files/figure-html/cell-22-output-1.png)
 MPL tips_files/figure-html/cell-25-output-2.png)
 MPL tips_files/figure-html/cell-27-output-1.png)